oldhourid = 0;
oldweeknr = 0;
hoursChoserName = "";

function voegtoe(type)
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="funcs.php?";
		url += "func=add" + type; // week OR teacher OR place OR pupil OR cluster
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function selectweek(weeknr)
{
	document.title = "iCUP - Laden";
	if (document.getElementById("wk_" + oldweeknr))
		document.getElementById("wk_" + oldweeknr).className="wk_button";
	oldweeknr = weeknr;
	document.getElementById("wk_" + weeknr).className="wk_button_selected";
	
	xmlhttp_ws = GetXmlHttpObject();
	
	if (xmlhttp_ws == null) return;
	
	var url = "beheerfuncs.php?";
		url += "selectweek";
		url += "&week=" + weeknr;
	
	xmlhttp_ws.onreadystatechange=weekSelectedCheckState;
	xmlhttp_ws.open("GET", url, true);
	xmlhttp_ws.send(null);
	
	
	xmlhttp = GetXmlHttpObject();
	
	url = "beheerfuncs.php?";
	url += "gethours";
	url += "&week=" + weeknr;
	
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function weekSelectedCheckState()
{
	document.title == ".";
	if (xmlhttp_ws.readyState == 4)
	{
		document.getElementById("hours").innerHTML = xmlhttp_ws.responseText;
		document.title = "iCUP - Beheer";
	}
}

function gethours(weekid)
{
	document.title = "iCUP - Laden";
	if (document.getElementById("su_" + oldhourid))
		document.getElementById("su_" + oldhourid).className="wk_button";
	oldhourid = weekid;
	document.getElementById("su_" + weekid).className="wk_button_selected";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
	url += "gethours";
	url += "&id=" + weekid;
	//url += "&klas=";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function addentireweek()
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="funcs.php?";
		url += "func=addweek";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function delentireweek()
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
		url += "delweek";
		url += "&week=" + oldweeknr;
	var he_is_not_sure = !confirm("wilt u echt week " + oldweeknr + " verwijderen?");
	if (he_is_not_sure) 
	{
		document.title = "iCUP - Beheer";
		return;
	}
	xmlhttp.onreadystatechange=stateChangedReload;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function editentireweek()
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
	url += "editweek";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function addentirehour()
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
		url += "addhour=new";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function delentirehour()
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
		url += "delhour";
		url += "&hourid=" + oldhourid;
	var he_is_not_sure = !confirm("wilt u het uur " + oldhourid + " echt verwijderen?");
	if (he_is_not_sure) 
	{
		document.title = "iCUP - Beheer";
		return;
	}
	xmlhttp.onreadystatechange=stateChangedReload;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function editentirehour()
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
		url += "edithour";
		url += "&id=" + oldweeknr;
	xmlhttp.onreadystatechange=stateChangedReload;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function addplacetohour(clustername, placenr)
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
		url += "addplace";
		url += "&class=" + clustername;
		url += "&placenr=" + placenr;
		
		//alert("url = " + url);
	xmlhttp.onreadystatechange=stateChangedReloadHours;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function addplacetoweek(hourid, placenr)
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
		url += "addplace";
		url += "&hourid=" + hourid;
		url += "&placenr=" + placenr;
		
		//alert("url = " + url);
	xmlhttp.onreadystatechange=stateChangedReloadWeek;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function delhour(hourname, hourid)
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	
	var values = "";
		values += "'" + document.getElementById("addHourRoomNr_" + hourname).value + "', ";
		values += "'" + document.getElementById("addHourTeacher_" + hourname).value + "', ";
		values += "'" + document.getElementById("addHourSubject_" + hourname).value + "', ";
		
	
	var url="beheerfuncs.php?";
		url += "delhour";
		url += "&values=" + values;
		url += "&hourid=" + hourid;
		
	xmlhttp.onreadystatechange = stateChangedReloadWeek;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function addhour(hourname, hourid)
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	
	var values = "";
		values += "'" + document.getElementById("addHourRoomNr_" + hourname).value + "', ";
		values += "'" + document.getElementById("addHourTeacher_" + hourname).value + "', ";
		values += "'" + document.getElementById("addHourSubject_" + hourname).value + "', ";
		
	
	var url="beheerfuncs.php?";
		url += "addhour";
		url += "&values=" + values;
		url += "&hourid=" + hourid;
		
	xmlhttp.onreadystatechange = stateChangedReloadWeek;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function delplacefromhour(clustername, placenr)
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
		url += "delplace";
		url += "&class=" + clustername;
		url += "&placenr=" + placenr;
		
		//alert("url = " + url);
	xmlhttp.onreadystatechange=stateChangedReloadHours;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function delplacefromweek(hourid, placenr)
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
		url += "delplace";
		url += "&hourid=" + hourid;
		url += "&placenr=" + placenr;
		
		//alert("url = " + url);
	xmlhttp.onreadystatechange=stateChangedReloadWeek;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function stateChangedReload()
{
	document.title == ".";
	if (xmlhttp.readyState == 4)
	{
		if (xmlhttp.responseText != '') alert(xmlhttp.responseText);
		location.reload(true);
	}
}

function stateChangedReloadWeek()
{
	document.title == ".";
	if (xmlhttp.readyState == 4)
	{
		if (xmlhttp.responseText != '') alert(xmlhttp.responseText);
		selectweek(oldweeknr);
	}
}

function stateChangedReloadHours()
{
	document.title == ".";
	if (xmlhttp.readyState == 4)
	{
		if (xmlhttp.responseText != '') alert(xmlhttp.responseText);
		gethours(oldhourid);
	}
}

function editteachers()
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
	url += "editweekdefault";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function editpupils()
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
	url += "editpupils";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function editclasses()
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	var url="beheerfuncs.php?";
	url += "editclasses";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function stateChanged()
{
	document.title == ".";
	if (xmlhttp.readyState == 4)
	{
		//alert(xmlhttp.responseText);
		document.getElementById("content").innerHTML = xmlhttp.responseText;
		document.title = "iCUP - Beheer";
	}
}

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE6, IE5
		alert("This site is not made for the browser you use, but I hope it will work.");
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Je browser ondersteunt geen AJAX!");
		document.title = "iCUP - Fout";
		return null;
	}
}

function newClass(id, name)
{
  document.getElementById(id).className=name;
}

function getPosition( oElementId, oParentTagname )
{
	var useClient = (cElement.offsetTop == 0) ? 
        ((cElement.offsetParent.tagName == "tr") ? false : true) : false
    var aReturnValue = [0, 0];
	var oElement = document.getElementById(oElementId);
	if (useClient) 
	{
		var aReturnValue = [cElement.clientLeft, cElement.clientTop];
    } 
	else 
	{
        var aReturnValue = [cElement.offsetLeft, cElement.offsetTop];
    }
    while( oElement != document.body ) 
	{
		if (useClient) {
            aReturnValue[0] += pElement.clientLeft;
            aReturnValue[1] += pElement.clientTop;
        } 
		else 
		{
            aReturnValue[0] += pElement.offsetLeft;
            aReturnValue[1] += pElement.offsetTop;
        }
        oElement = oElement.offsetParent;
	}
	return aReturnValue;
}

function expchoser_click(xtraOT, rndId)
{
	document.getElementById('choser').style.visibility = 'visible'; 
	if (navigator.appVersion.indexOf("MSIE 7.0") != -1)
	{
		document.getElementById('choser').style.left = (document.getElementById(rndId).offsetLeft - 6) + 'px'; 
		document.getElementById('choser').style.top = (document.getElementById(rndId).offsetTop + xtraOT + 11) + 'px';
	}
	else
	{
		document.getElementById('choser').style.left = (document.getElementById(rndId).offsetLeft - 5) + 'px'; 
		document.getElementById('choser').style.top = (document.getElementById(rndId).offsetTop + xtraOT) + 'px';
	}
}

function reloadHoursChoser(hourname, hourid)
{
	document.title = "iCUP - Laden";
	hoursChoserName = hourname;
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	
	var url="gethoursChoser.php";
		url += "?header=" + hourname;
		url += "&hourid=" + hourid;
		url += "&subject=" + document.getElementById("addHourSubject_" + hourname).value;
		url += "&docentid=" + document.getElementById("addHourTeacher_" + hourname).value;
		url += "&classroom=" + document.getElementById("addHourRoomNr_" + hourname).value;
		
	xmlhttp.onreadystatechange = stateChangedReloadHoursChoser;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function stateChangedReloadHoursChoser()
{
	document.title += ".";
	if (xmlhttp.readyState == 4)
	{
		//alert(xmlhttp.responseText);
		document.getElementById("choser_" + hoursChoserName).getElementsByTagName('td')[0].innerHTML = xmlhttp.responseText;
		document.title = "iCUP - Beheer";
	}
}

function clearadderfield(hourname)
{
	document.getElementById("addHourSubject_" + hourname).value = "";
	document.getElementById("addHourTeacher_" + hourname).value = "";
	document.getElementById("addHourRoomNr_" + hourname).value = "";
}

function reloadGethours_week()
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	
	var url="gethours_week.php";
		url += "?week=" + oldweeknr;
		url += "&hour=" + document.getElementById("reloadGethours_week").value;
		
	xmlhttp.onreadystatechange = stateChangedReloadGethours_week;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function stateChangedReloadGethours_week()
{
	document.title += ".";
	if (xmlhttp.readyState == 4)
	{
		//alert(xmlhttp.responseText);
		document.getElementById("gethours_week").innerHTML = xmlhttp.responseText;
		document.title = "iCUP - Beheer";
	}
}

function reloadGethours_id()
{
	document.title = "iCUP - Laden";
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) return;
	
	var url="gethours_id.php";
		url += "?id=" + oldhourid;
		url += "&class=" + document.getElementById("reloadGethours_id").value;
		
	xmlhttp.onreadystatechange = stateChangedReloadGethours_id;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function stateChangedReloadGethours_id()
{
	document.title += ".";
	if (xmlhttp.readyState == 4)
	{
		//alert(xmlhttp.responseText);
		document.getElementById("gethours_id").innerHTML = xmlhttp.responseText;
		document.title = "iCUP - Beheer";
	}
}

function enter_was_pressed(e)
{
	return (window.event ? e.keyCode : e.which) == 13;
}

